package com.hongyuan.sellcoach.utils;


import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.hongyuan.mvvmhabitx.utils.ToastUtils;
import com.hongyuan.sellcoach.R;
import com.hongyuan.sellcoach.data.md5.BaseUtil;
import com.hongyuan.sellcoach.myview.date_period.CalendarView;
import com.hongyuan.sellcoach.myview.date_period.DateUtils;
import com.hongyuan.sellcoach.myview.scllor_view.UnitBean;
import com.hongyuan.sellcoach.myview.scllor_view.UnitPicker;

import java.util.Date;
import java.util.List;

public class CustomDialog {
    /*
     * 点击事件的回调 接口
     * */
    public interface DialogClick{
        void dialogClick(View v);
    }

    /*
     * 用于修改的接口回调
     * */
    public interface DialogClickMessage{
        void dialogClick(View v, String message);
    }

    /*
     * 用于修改的接口回调
     * */
    public interface DialogClickTwoMessage{
        void dialogClick(View v, String message1,String message2);
    }

    /*
     * 用于修改的接口回调
     * */
    public interface DialogClickMessageAndId{
        void dialogClick(View v, String message,String id);
    }

    /*
     * 拨打电话弹框
     * */
    public static void callTel(Context mContext, String telNum , DialogClick dialogClick ){
        final Dialog dialog = new Dialog(mContext, R.style.DialogTheme);
        View view = View.inflate(mContext, R.layout.dialog_call_tel,null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.main_menu_animStyle);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        TextView telContent= view.findViewById(R.id.telContent);
        telContent.setText(telNum);
        view.findViewById(R.id.cancel).setOnClickListener(v -> dialog.dismiss());
        view.findViewById(R.id.call).setOnClickListener(v -> {
            if(dialogClick != null){
                dialogClick.dialogClick(v);
            }
            dialog.dismiss();
        });
    }

    /*
     * 查看H5 url连接
     * */
    public static void showUrl(Context mContext, String url){
        final Dialog dialog = new Dialog(mContext, R.style.DialogTheme);
        View view = View.inflate(mContext, R.layout.dialog_show_url,null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.main_menu_animStyle);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        TextView tv = view.findViewById(R.id.urlTv);
        tv.setText(url);
    }


    /*
     * 1.普通对话弹框
     * 2.有两个取消和确定两个按钮，并按钮有回调。
     * */
    public static void promptDialog(Context mContext,String messagesText,String isOkText,String isCannelText,boolean outSide,DialogClick dialogClick){
        final Dialog dialog = new Dialog(mContext, R.style.DialogTheme);
        View view = View.inflate(mContext, R.layout.dialog_have_yes_no_prompt,null);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(outSide);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.main_menu_animStyle);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        TextView messages = view.findViewById(R.id.messages);
        TextView isOk = view.findViewById(R.id.isOk);
        TextView isCannel = view.findViewById(R.id.isCannel);

        isOk.setText(isOkText);
        isCannel.setText(isCannelText);
        messages.setText(messagesText);

        isOk.setOnClickListener(v -> {
            if(dialogClick != null){
                dialogClick.dialogClick(v);
            }
            dialog.dismiss();
        });
        isCannel.setOnClickListener(v -> {
            if(dialogClick != null){
                dialogClick.dialogClick(v);
            }
            dialog.dismiss();
        });
    }

    /*
     * 1.普通对话弹框
     * 2.单个按钮
     * */
    public static void promptDialog(Context mContext,String messagesText,String isOkText,boolean outSide,DialogClick dialogClick){
        final Dialog dialog = new Dialog(mContext, R.style.DialogTheme);
        View view = View.inflate(mContext, R.layout.dialog_have_yes_no_prompt,null);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(outSide);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.main_menu_animStyle);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        TextView messages = view.findViewById(R.id.messages);
        TextView isOk = view.findViewById(R.id.isOk);
        TextView isCannel = view.findViewById(R.id.isCannel);
        View line = view.findViewById(R.id.line);
        line.setVisibility(View.GONE);
        isCannel.setVisibility(View.GONE);

        isOk.setText(isOkText);
        messages.setText(messagesText);

        isOk.setOnClickListener(v -> {
            if(dialogClick != null){
                dialogClick.dialogClick(v);
            }
            dialog.dismiss();
        });
        isCannel.setOnClickListener(v -> {
            if(dialogClick != null){
                dialogClick.dialogClick(v);
            }
            dialog.dismiss();
        });
    }


    /*
     * 滚动选择控件
     * */
    public static void scroller(Context mContext, List<UnitBean> mList, String titleText , DialogClickMessageAndId dialogClick ){
        final Dialog dialog = new Dialog(mContext, R.style.DialogTheme);
        View view = View.inflate(mContext, R.layout.dialog_scroller,null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.bottom_in_out);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        TextView titleView= view.findViewById(R.id.titleName);
        UnitPicker content = view.findViewById(R.id.content);
        content.setData(mList);
        titleView.setText(titleText);
        view.findViewById(R.id.closeImg).setOnClickListener(v -> dialog.dismiss());
        view.findViewById(R.id.submit).setOnClickListener(v -> {
            if(dialogClick != null){
                dialogClick.dialogClick(v,content.getCurrentUnit().unit_cn,content.getCurrentUnit().unit);
            }
            dialog.dismiss();
        });
    }

    /*
     * 驳回输入框
     * */
    public static void showTurnDowen(Context mContext, DialogClickMessage dialogClick ){
        final Dialog dialog = new Dialog(mContext, R.style.DialogTheme);
        View view = View.inflate(mContext, R.layout.dialog_turn_down,null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.CENTER);
        window.setWindowAnimations(R.style.bottom_in_out);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        EditText etText= view.findViewById(R.id.etText);
        view.findViewById(R.id.submit).setOnClickListener(v -> {
            if(BaseUtil.isValue(etText.getText().toString())){
                dialogClick.dialogClick(v,etText.getText().toString());
                dialog.dismiss();
            }else{
                ToastUtils.showShort("请输入驳回理由");
            }
        });
    }

    /*
    * 时间段选择器
    * */
    public static void selectDatePeriod(Context mContext , DialogClickTwoMessage dialogClick ){

        final Dialog dialog = new Dialog(mContext, R.style.DialogTheme);
        View view = View.inflate(mContext, R.layout.dialog_date_period,null);
        dialog.setContentView(view);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.bottom_in_out);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

        TextView starTime = view.findViewById(R.id.starTime);
        TextView endTime = view.findViewById(R.id.endTime);
        TextView submit = view.findViewById(R.id.submit);
        CalendarView calendarView = view.findViewById(R.id.calendarview);

        view.findViewById(R.id.cancel).setOnClickListener(v -> {
            dialogClick.dialogClick(v,"所有时间段","所有时间段");
            dialog.dismiss();
        });
        submit.setOnClickListener(v -> {
            if(dialogClick != null && submit.getTag() != null && (boolean)submit.getTag()){
                dialogClick.dialogClick(v,starTime.getText().toString(),endTime.getText().toString());
                dialog.dismiss();
            }else{
                ToastUtils.showShort("请选择结束时间");
            }
        });

        calendarView.setETimeSelListener(date -> {
            if (date != null) {
                endTime.setText(DateUtils.formatData(date, TimeUtil.dateFormatDotYMD));
            }else {
                endTime.setText("开始时间");
            }
        });
        calendarView.setSTimeSelListener(date -> {
            if (date != null) {
                starTime.setText(DateUtils.formatData(date, TimeUtil.dateFormatDotYMD));
            }else {
                starTime.setText("结束时间");
            }
        });
        calendarView.setCalendaSelListener(isOk -> submit.setTag(isOk));
    }

}
